CC ?= gcc
AR ?= ar

CFLAGS	= -std=c11 -Wall -Wextra -Wpedantic -Werror -g
# 注意需要指定 -static
LDFLAGS = -static
ARFLAGS = rcs

OUTPUT_DIR ?= .

SRCS		= main.c
OBJS		= $(SRCS:.c=.o)
TARGET		= $(OUTPUT_DIR)/exercise-03

TEST_SRCS	= test.c
TEST_OBJS	= $(TEST_SRCS:.c=.o)
TEST_TARGET = $(OUTPUT_DIR)/exercise-03_test

STATIC_SRCS = functions.c
STATIC_OBJS	= $(STATIC_SRCS:.c=.o)
STATIC_TARGET = $(STATIC_SRCS:.c=.a)

all: $(TARGET)

$(TARGET): $(OBJS) $(STATIC_TARGET)
	$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $(TARGET)

$(TEST_TARGET): $(TEST_OBJS) $(STATIC_TARGET)
	$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $(TEST_TARGET)

$(STATIC_TARGET): $(STATIC_OBJS)
	$(AR) $(ARFLAGS) $@ $^

%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

test: $(TEST_TARGET)
	./$(TEST_TARGET)

clean:
	rm -f $(TARGET) $(TEST_TARGET) $(STATIC_TARGET) $(OBJS) $(TEST_OBJS) $(STATIC_OBJS)
